<template>
  <div id="ResetPassword">
    <div class="ResetBody">
      <p class="title">重置密码</p>
      <ul class="stepList">
        <li class="stepItem" :class="active == 1 ? 'activeItem' : ''">
          <span class="stepNumber">1</span>
          <span class="stepText">输入手机号</span>
        </li>
        <li class="stepItem stepItem_hx"></li>
        <li class="stepItem" :class="active == 2 ? 'activeItem' : ''">
          <span class="stepNumber">2</span>
          <span class="stepText">确认账号</span>
        </li>
        <li class="stepItem stepItem_hx"></li>
        <li class="stepItem" :class="active == 3 ? 'activeItem' : ''">
          <span class="stepNumber">3</span>
          <span class="stepText">重置密码</span>
        </li>
      </ul>
      <div class="reg-zc" :style="active == 2 ? 'margin: 60px 0 30px 0' : ''">
        <el-form ref="form" :model="form" :rules="rules" label-width="0px" v-show="active != 2">
          <el-form-item label="" prop="phone" v-if="active == 1">
            <el-input v-model="form.phone" placeholder="请输入手机号">
              <template #prefix>
                <img class="label_icon" src="../../../assets/images/shouji.png" alt="" srcset="" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="" prop="code" v-if="active == 1">
            <el-input class="phoneStyle" v-model="form.code" placeholder="请输入短信验证码">
              <template #prefix>
                <img class="label_icon" src="../../../assets/images/duanxin.png" alt="" srcset="" />
              </template>
            </el-input>
            <el-button class="btn" type="primary" @click="getCode(form.phone, 'loginApi', 'GetPhoneCode', '', 1)" :disabled="phonedisabled">{{ btnText }}</el-button>
            <captcha-account ref="captchaAccount" style="margin-top: 27px" />
          </el-form-item>

          <el-form-item label="" prop="password" v-if="active == 3">
            <el-input v-model="form.password" show-password placeholder="请输入新密码"></el-input>
          </el-form-item>
          <el-form-item label="" prop="passwords" v-if="active == 3">
            <el-input v-model="form.passwords" show-password placeholder="请确认新密码"></el-input>
          </el-form-item>
        </el-form>
        <div v-show="active == 2" class="companyClass">
          <div class="company" :class="active1 === index ? 'active1Class' : ''" v-for="(item, index) in companyList" :key="item.userId + index" @click="checkboxCompany(index)">
            <p class="name">{{ item.companyname }}</p>
            <div>
              <img class="logo" :src="item.headPortrait" v-if="item.headPortrait" alt="" srcset="" />
              <span class="logo logo_text" v-else>{{ item.realName.slice(item.realName.length - 2, item.realName.length) }}</span>
              <span class="name_right">
                <span class="realName">{{ item.realName }}</span>
                <span class="position">{{ item.position }}</span>
              </span>
            </div>
          </div>
        </div>
      </div>
      <div class="footer">
        <el-button type="primary" v-show="active != 2" :loading="isConfirmLoading" @click="ConfirmResetFun('form')">确认重置</el-button>
        <el-button type="primary" v-show="active == 2" :loading="isConfirmLoading" @click="nextStep">下一步</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import * as Vrules from "@/util/validate";
import useGetCode from "../mixins/useGetCode";
import md5 from "js-md5";
import captchaAccount from "./captchaAccount.vue";
export default {
  name: "ResetPassword",
  mixins: [useGetCode],
  components: { captchaAccount },
  data() {
    let ISpassword = (rule, value, callback) => {
      setTimeout(() => {
        if (this.form.passwords) {
          if (this.form.password != this.form.passwords) {
            callback(new Error("两次密码不一致!"));
          } else {
            callback();
          }
        } else {
          callback();
        }
      }, 500);
    };
    let ISpasswords = (rule, value, callback) => {
      setTimeout(() => {
        if (this.form.password) {
          if (this.form.password != this.form.passwords) {
            callback(new Error("两次密码不一致!"));
          } else {
            callback();
          }
        } else {
          callback();
        }
      }, 500);
    };
    return {
      isConfirmLoading: false,
      active: 1,
      form: {
        phone: "",
        password: "",
        passwords: "",
        code: "",
      },
      subId: "",
      userId: "", //公司id
      companyList: [], // 公司
      active1: "",
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "change" },
          { validator: Vrules.isPhone, trigger: "change" },
        ],
        code: [{ required: true, message: "请输入验证码", trigger: "change" }],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          { pattern: /^(?=.*[0-9].*)(?=.*[A-z].*).{8,}$/, message: "密码至少八位  且密码中必须包含字母,数字", trigger: "change" },
          { validator: ISpassword, trigger: "change" },
        ],
        passwords: [
          { required: true, message: "请再次输入密码", trigger: "change" },
          { validator: ISpasswords, trigger: "change" },
        ],
      },
    };
  },
  methods: {
    againGetCode(data) {
      if (data) {
        this.getCode(this.form.phone, "loginApi", "GetPhoneCode", data, 1);
      } else {
        // if (!this.$refs.captchaAccount.submit()) return;
      }
    },
    getCode(phone, apiModule, api, nvcVal2, type) {
      // if (type == 1) {
      //   if (nvcVal2) {
      //     setTimeout(() => {
      //       this.$refs.captchaAccount.complete();
      //     }, 200);
      //   } else {
      //     if (!this.$refs.captchaAccount.submit()) return;
      //   }
      // }
      if (this.phonedisabled || this.isCodeloading) return;
      let isMob = /^1[3,4,5,6,7,8,9]\d{9}$/;
      if (isMob.test(phone)) {
        let params = {
          phone: phone,
        };
        if (type == 1) params.machineVal = nvcVal2 || window.nvc.getNVCVal() || "";
        this.isCodeloading = true;
        this.$api[apiModule][api](params)
          .then((res) => {
            this.isCodeloading = false;
            if (type == 1) {
              this.setText();
              // this.$refs.captchaAccount.verify(
              //   { data: res.data, slide: "验证" },
              //   () => {
              //     this.setText();
              //   },
              //   () => {
              //     this.isCode = true;
              //   }
              // );
            } else this.setText();
          })
          .catch((res) => {
            this.isCodeloading = false;
            this.$emit("verification", true);
            // this.$refs.captchaAccount.verify(
            //   { data: res.data },
            //   () => {},
            //   () => {}
            // );
          });
      } else {
        this.$message({ message: "请输入合法手机号", type: "warning" });
      }
    },
    checkboxCompany(i) {
      // 选择企业
      this.active1 = i;
    },
    ConfirmResetFun(formName) {
      // 确认重置
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.active == 1) {
            let params = {
              code: this.form.code,
              phone: this.form.phone,
            };
            this.isConfirmLoading = true;
            this.$api.loginApi
              .GetUserByMobile(params)
              .then((res) => {
                let { data, message, status } = res.data;
                if (status == 200) {
                  this.active = 2;
                  this.companyList = data;
                }
                this.isConfirmLoading = false;
              })
              .catch(() => {
                this.isConfirmLoading = false;
              });
          } else if (this.active == 3) {
            let params = {
              userId: this.userId,
              subId: this.subId,
              pwd: md5(this.form.password),
            };
            this.isConfirmLoading = true;
            this.$api.loginApi
              .ResetPwd(params)
              .then((res) => {
                let { data, message, status } = res.data;
                if (status == 200) {
                  this.$message({ type: "success", message: "重置成功" });
                  this.$emit("Reset");
                  this.active = 1;
                  this.form = {
                    phone: "",
                    password: "",
                    passwords: "",
                    code: "",
                  };
                  this.companyList = [];
                }
                this.isConfirmLoading = false;
              })
              .catch(() => {
                this.isConfirmLoading = false;
              });
          }
        }
      });
    },
    nextStep() {
      console.log(this.active1, "active1");
      if (this.active1 || this.active1 === 0) {
        this.active = 3;
        this.userId = this.companyList[this.active1].userId;
        this.subId = this.companyList[this.active1].subId; 
      } else {
        this.$message({ type: "warning", message: "请选择要修改密码的企业" });
      }
    },
  },
};
</script>

<style lang="less">
#ResetPassword {
  @media screen and (min-width: 0px) and (max-width: 1400px) {
    .title {
      font-size: 24px;
      font-weight: bold;
      margin: 10px;
    }
    .stepList {
      margin-top: 40px;
    }
    .reg-zc {
      margin: 30px 0px 30pxz;
      .companyClass {
        .name_right {
          font-size: 14px;
          padding-left: 10px !important;
          padding-top: 5px !important;
        }
        .company {
          height: 28%;
          border-bottom: 1px solid #efefef;
          padding: 0 20px;
          .name {
            padding: 5px 0;
          }
          .logo {
            float: left;
            width: 30px;
            border-radius: 50%;
          }
          .logo_text {
            display: inline-block;
            height: 30px;
            line-height: 30px;
            background-color: #39a4ef;
            text-align: center;
            color: #fff;
          }
        }
      }
    }
  }
  @media screen and (min-width: 1401px) {
    .title {
      font-size: 24px;
      font-weight: bold;
      margin: 40px 0 30px 0;
    }
    .reg-zc {
      .companyClass {
        .company {
          height: 50%;
          border-bottom: 1px solid #efefef;
          padding: 0 20px;
          .name {
            padding: 20px 0;
          }
          .logo {
            float: left;
            width: 50px;
            border-radius: 50%;
          }
          .logo_text {
            display: inline-block;
            height: 50px;
            line-height: 50px;
            background-color: #39a4ef;
            text-align: center;
            color: #fff;
          }
        }
      }
    }
  }
  width: 100%;
  height: calc(100% - 65px);
  background-color: #f5f6fa;
  // display: flex;
  // justify-content: center;
  // align-items: center;
  position: relative;
  .ResetBody {
    width: 80%;
    height: 80%;
    min-width: 630px;
    display: flex;
    flex-direction: column;
    align-items: center;
    background-color: #fff;
    margin: auto;
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    left: 0;
    box-shadow: 0px 0px 30px rgb(224, 224, 224);
  }
  .stepList {
    width: 630px;
    .stepItem {
      float: left;
      display: flex;
      flex-direction: column;
      align-items: center;
      cursor: pointer;
      .stepNumber {
        width: 28px;
        height: 28px;
        border-radius: 50%;
        background-color: #c9d0e0;
        text-align: center;
        color: #fff;
        line-height: 28px;
        font-size: 14px;
      }
      .stepText {
        margin-top: 14px;
        color: #333;
        font-size: 14px;
      }
    }
    .activeItem {
      .stepNumber {
        background-color: #39a4ef;
      }
      .stepText {
        color: #39a4ef;
      }
    }
    .stepItem_hx {
      width: 220px;
      height: 1px;
      background-color: #ccc;
      position: relative;
      top: 14px;
    }
  }
  .reg-zc {
    width: 630px;
    margin: 80px 0 60px 0;
    .phoneStyle {
      width: calc(100% - 88px);
    }
    .companyClass {
      height: 280px;
      overflow-y: auto;
      border: 1px solid #efefef;
      color: #333;
      .name_right {
        display: flex;
        flex-direction: column;
        padding-left: 13px;
        .realName {
        }
        .position {
        }
      }
    }
    .pitch {
      background-color: #efefef;
    }
    .active1Class {
      background-color: #99999910;
    }
    .btn {
      height: 30px;
      border: none;
      background-color: #fff;
      color: #39a4ef;
    }
    .label_icon {
      width: 20px;
      margin-top: 6px;
    }
  }
  .footer {
    // width: 400px;
    // display: flex;
    // justify-content: space-between;
    .el-button {
      width: 630px;
      height: 44px;
      font-size: 14px;
      border-radius: 6px;
      background-color: #39a4ef;
    }
  }
}
</style>
